home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Application / Examples / Harvest MiniEdit / BuggyEdit.c next >
Encoding:
C/C++ Source or Header  |  1993-06-20  |  6.8 KB  |  339 lines  |  [TEXT/ALFA]

  1. /*********************************************************************
  2.  
  3.     Buggy MiniEdit.c
  4.     
  5.     The sample application from Inside Macintosh (RoadMap p.15-17)
  6.     beefed up a bit by Stephen Z. Stein, Symantec Corp.
  7.     Use this file with the “MiniEdit” chapter of your manual.
  8.     
  9.     The resources used in this program are in the file MiniEdit.π.rsrc.
  10.     
  11.     In order for THINK C to find the resource file for this
  12.     project, be sure you’ve named the project MiniEdit.π
  13.     
  14.     
  15. *********************************************************************/
  16.  
  17. #include <Windows.h>
  18. #include <Menus.h>
  19. #include <Resources.h>
  20. #include <Events.h>
  21. #include <Dialogs.h>
  22. #include <Desk.h>
  23. #include <Scrap.h>
  24. #include <OSEvents.h>
  25. #include <Memory.h>
  26. #include <Fonts.h>
  27. #include <TextEdit.h>
  28. #include <ToolUtils.h>
  29. #include <Errors.h>
  30. #include <Files.h>
  31. #include "mini.file.h"
  32. #include "mini.windows.h"
  33. #include "MiniEdit.h"
  34.  
  35.  
  36.  
  37. WindowRecord    wRecord;
  38. WindowPtr        myWindow;
  39. TEHandle        TEH;
  40. int                linesInFolder;
  41. Rect            dragRect = { 0, 0, 1024, 1024 };
  42. MenuHandle        myMenus[3];
  43. ControlHandle     vScroll;
  44. Cursor            editCursor;
  45. Cursor            waitCursor;
  46. char            dirty;
  47.  
  48. extern Str255     theFileName;
  49.  
  50. #define    ours(w)        ((myWindow != NULL) && (w == myWindow))
  51.  
  52.  
  53. main() 
  54. {
  55.     int        myRsrc;
  56.     
  57.     InitGraf(&(qd.thePort));
  58.     InitFonts();
  59.     FlushEvents(everyEvent, 0);
  60.     InitWindows();
  61.     InitMenus();
  62.     TEInit();
  63.     InitDialogs(0L);
  64.     InitCursor();
  65.     MaxApplZone();
  66.  
  67. /*
  68. /*  The following statement is included as a check to see if we can
  69. /*    access our program's resources.  When the project is run from
  70. /*    THINK C, the resource file <project name>.rsrc is automatically
  71. /*  opened.  When an application is built, these resources are 
  72. /*     automatically merged with the application.
  73. /*
  74. */
  75.     
  76.     if (GetResource('MENU', fileID)==0) {
  77.         SysBeep(20);
  78.         CantOpen();
  79.         return 0;
  80.     }
  81.     
  82.     SetUpFiles();
  83.     SetUpCursors();
  84.     SetUpMenus();
  85.     SetUpWindows();
  86.     while (MainEvent()) ;
  87. }
  88.  
  89.  
  90.  
  91. int DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  92.  
  93. {
  94.     switch (windowPart) {
  95.         case inGoAway:
  96.             if (ours(whichWindow))
  97.                 if (TrackGoAway(myWindow, myEvent->where))
  98.                     DoFile(fmClose);
  99.             break;
  100.  
  101.         case inMenuBar:
  102.             return(DoCommand(MenuSelect(myEvent->where)));
  103.  
  104.         case inSysWindow:
  105.             SystemClick(myEvent, whichWindow);
  106.             break;
  107.  
  108.         case inDrag:
  109.             if (ours(whichWindow))
  110.                 DragWindow(whichWindow, myEvent->where, &dragRect);
  111.             break;
  112.  
  113.         case inGrow:
  114.             if (ours(whichWindow))
  115.                 MyGrowWindow(whichWindow, myEvent->where);
  116.             break;
  117.  
  118.         case inContent:
  119.             if (whichWindow != FrontWindow())
  120.                 SelectWindow(whichWindow);
  121.             else if (ours(whichWindow))
  122.                 DoContent(whichWindow, myEvent);
  123.             break;
  124.     }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. int MainEvent(void) 
  133. {
  134.     EventRecord        myEvent;
  135.     WindowPtr        whichWindow;
  136.     short            windowPart;
  137.     Rect            r;
  138.     
  139.     MaintainCursor();
  140.     MaintainMenus();
  141.     SystemTask();
  142.     TEIdle(TEH);
  143.     if (GetNextEvent(everyEvent, &myEvent)) {
  144.         switch (myEvent.what) {
  145.         case mouseDown:
  146.             windowPart = FindWindow(myEvent.where, &whichWindow);
  147.             DoMouseDown(windowPart, whichWindow, &myEvent);
  148.             break;
  149.  
  150.         case keyDown:
  151.         case autoKey: 
  152.             {
  153.             register char    theChar;
  154.             
  155.             theChar = myEvent.message & charCodeMask;
  156.             if ((myEvent.modifiers & cmdKey) != 0) 
  157.                 return(DoCommand(MenuKey( theChar)));
  158.             else {
  159.                 TEKey(theChar, TEH);
  160.                 ShowSelect();
  161.                 dirty = 1;
  162.                 }
  163.             }
  164.             break;
  165.  
  166.         case activateEvt:
  167.             if (ours((WindowPtr)myEvent.message)) {
  168.                 if (myEvent.modifiers & activeFlag) {
  169.                     TEActivate(TEH);
  170.                     ShowControl(vScroll);
  171.                     DisableItem(myMenus[editM], undoCommand);
  172.                     TEFromScrap();
  173.                 }
  174.                 else {
  175.                     TEDeactivate(TEH);
  176.                     HideControl(vScroll);
  177.                     ZeroScrap();
  178.                     TEToScrap();
  179.                 }
  180.             }
  181.             break;
  182.  
  183.         case updateEvt: 
  184.             if (ours((WindowPtr) myEvent.message))
  185.                     UpdateWindow(myWindow);
  186.             break;
  187.         } /* end of case myEvent.what */
  188.     } /* if */
  189.     return(1);
  190. }
  191.  
  192. int SetUpMenus(void)
  193. {
  194.     int        i;
  195.     
  196.     myMenus[appleM] = NewMenu(appleID, "\p\024");
  197.     AddResMenu(myMenus[appleM], 'DRVR');
  198.     myMenus[fileM] = GetMenu(fileID);
  199.     myMenus[editM] = GetMenu(editID);
  200.     for ((i=appleM); (i<=editM); i++)
  201.         InsertMenu(myMenus[i], 0) ;
  202.     DrawMenuBar();
  203. }
  204.  
  205. int DoCommand(long mResult)
  206.  
  207. {
  208.     int        theItem;
  209.     Str255    name;
  210.     
  211.     theItem = LoWord(mResult);
  212.     switch (HiWord(mResult)) {
  213.         case appleID:
  214.             GetItem(myMenus[appleM], theItem, &name);
  215.             OpenDeskAcc(name);
  216.             SetPort(myWindow);
  217.             break;
  218.  
  219.         case fileID: 
  220.             DoFile(theItem);
  221.             break;
  222.  
  223.         case editID: 
  224.             if (SystemEdit(theItem-1) == 0) {
  225.                 switch (theItem) {
  226.                     case cutCommand:
  227.                         TECut(TEH);
  228.                         dirty = 1;
  229.                         break;
  230.     
  231.                     case copyCommand:
  232.                         TECopy(TEH);
  233.                         break;
  234.         
  235.                     case pasteCommand:
  236.                         TEPaste(TEH);
  237.                         dirty = 1;
  238.                         break;
  239.         
  240.                     case clearCommand:
  241.                         TEDelete(TEH);
  242.                         dirty = 1;
  243.                         break;
  244.                 }
  245.                 ShowSelect();
  246.             }
  247.             break;
  248.     }
  249.     HiliteMenu(0);
  250.     return(1);
  251. }
  252.  
  253. int MaintainCursor(void)
  254. {
  255.     Point        pt;
  256.     WindowPeek    wPtr;
  257.     GrafPtr        savePort;
  258.     
  259.     if (ours((WindowPtr)(wPtr=(WindowPeek)FrontWindow()))) {
  260.         GetPort(&savePort);
  261.         SetPort((GrafPtr)wPtr);
  262.         GetMouse(&pt);
  263.         if (PtInRect(pt, &(**TEH).viewRect ) )
  264.             SetCursor( &editCursor);
  265.         else SetCursor(&qd.arrow);
  266.         SetPort(savePort);
  267.     }
  268. }
  269.  
  270. int MaintainMenus(void)
  271. {
  272.     if ( !(*(WindowPeek)myWindow).visible || 
  273.             !ours(FrontWindow()) ) {
  274.         EnableItem(myMenus[fileM], fmNew);
  275.         EnableItem(myMenus[fileM], fmOpen);
  276.         DisableItem(myMenus[fileM], fmClose);
  277.         DisableItem(myMenus[fileM], fmSave);
  278.         DisableItem(myMenus[fileM], fmSaveAs);
  279.         DisableItem(myMenus[fileM], fmRevert);
  280.         DisableItem(myMenus[fileM], fmPrint);
  281.         EnableItem(myMenus[editM], undoCommand);
  282.         EnableItem(myMenus[editM], cutCommand);
  283.         EnableItem(myMenus[editM], copyCommand);
  284.         EnableItem(myMenus[editM], clearCommand);
  285.     }
  286.     else {
  287.         DisableItem(myMenus[fileM], fmNew);
  288.         DisableItem(myMenus[fileM], fmOpen);
  289.         EnableItem(myMenus[fileM], fmClose);
  290.         EnableItem(myMenus[fileM], fmSaveAs);
  291.         EnableItem(myMenus[fileM], fmPrint);
  292.         if (dirty && theFileName[0] != 0) {
  293.             EnableItem(myMenus[fileM], fmRevert);
  294.             EnableItem(myMenus[fileM], fmSave);
  295.         }
  296.         else {
  297.             DisableItem(myMenus[fileM], fmRevert);
  298.             DisableItem(myMenus[fileM], fmSave);
  299.         }
  300.         DisableItem(myMenus[editM], undoCommand);
  301.         if ((**TEH).selStart==(**TEH).selEnd) {
  302.             DisableItem(myMenus[editM], cutCommand);
  303.             DisableItem(myMenus[editM], copyCommand);
  304.             DisableItem(myMenus[editM], clearCommand);
  305.         }
  306.         else {
  307.             EnableItem(myMenus[editM], cutCommand);
  308.             EnableItem(myMenus[editM], copyCommand);
  309.             EnableItem(myMenus[editM], clearCommand);
  310.         }
  311.     }
  312. }
  313.  
  314. int SetUpCursors(void)
  315. {
  316.     CursHandle    hCurs;
  317.     
  318.     hCurs = GetCursor(1);
  319.     editCursor = **hCurs;
  320.     hCurs = GetCursor(watchCursor);
  321.     waitCursor = **hCurs;
  322. }
  323.  
  324.  
  325. int CantOpen(void)
  326. {
  327.     Rect r;
  328.  
  329.     SetRect(&r, 152, 60, 356, 132);
  330.     SetPort((myWindow = NewWindow( (Ptr) 0L, &r, "\p", true, dBoxProc, (WindowPtr) -1L, false, 0L)));
  331.     TextFont(0);
  332.     MoveTo(4, 20);
  333.     DrawString("\pCan't open resource file.");
  334.     MoveTo(4, 40);
  335.     DrawString("\pClick mouse to exit.");
  336.     do {
  337.     } while (!Button());
  338. }
  339.